﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimaryAlgorithm.MissingNumber
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] nums = new int[] { 3, 0, 1 };
            Console.WriteLine(MissingNumber(nums));
            Console.ReadKey();
        }

        //位运算
        public static int MissingNumber(int[] nums)
        {
            int rox = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                rox ^= nums[i] ^ (i + 1);
            }
            return rox;
        }

        //等差公式求和

        /// <summary>
        /// 最简单的思路
        /// </summary>
        /// <param name="nums"></param>
        /// <returns></returns>
        public static int MissingNumber2(int[] nums)
        {
            List<int> list = new List<int>();
            for (int i = 1; i <= nums.Length; i++)
                list.Add(i);
            for (int i = 0; i < nums.Length; i++)
            {
                if (list.Contains(nums[i]))
                {
                    list.Remove(nums[i]);
                }
            }
            if (list.Count==1)
            {
                return list[0];
            }
            return 0;
        }
    }
}
